using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using ModelPredictPro.WindowForms.UserControls;
using ModelPredictPro.WindowForms.AppCodes;

namespace ModelPredictPro.WindowForms {
    public partial class formMain : ComponentFactory.Krypton.Toolkit.KryptonForm {
        public formMain() {
            InitializeComponent();
        }
        formTaskList taskListForm = new formTaskList();
        KryptonCheckButton[] menus = null; 

        protected override void OnClosing(CancelEventArgs e) {
            taskListForm.Close();
            base.OnClosing(e);
        }

        #region style

        void CreateStyleMenus() {
            EnumBinder<PaletteModeManager> styleEnums = new EnumBinder<PaletteModeManager>();
            ToolStripMenuItem parent = styleSToolStripMenuItem;
            foreach (var item in styleEnums.Pairs) {
                ToolStripMenuItem menuItem = new ToolStripMenuItem(item.Name, null, new EventHandler(OnStyleMenuClick));
                menuItem.Tag = item.Enum;
                parent.DropDownItems.Add(menuItem);
            }
        }
        void OnStyleMenuClick(object sender, EventArgs e) {
            foreach (var item in styleSToolStripMenuItem.DropDownItems.Cast<ToolStripMenuItem>()) {
                item.Checked = false;
            }
            ToolStripMenuItem menu = sender as ToolStripMenuItem;
            menu.Checked = true;
            kryptonManager.GlobalPaletteMode = ((PaletteModeManager)menu.Tag);
        }

        #endregion

        private void Form1_Load(object sender, EventArgs e) {
            CreateStyleMenus();
            menus = new KryptonCheckButton[] { btnSVMTrain, btnSVMPredict, btnTrainPredict, btnFormatChecking, btnNFold };
            var ucControls = new UserControl[] { new UCTrainTask(), new UCPredictTask(), new UCTrainPredictTask(), new UCConsole(), new UCNFold() };
            for (int i = 0; i < menus.Length; i++) {
                UserControl uc =  ucControls[i];
                menus[i].Tag = uc;
                if (uc != null) {
                    uc.Dock = DockStyle.Fill;
                    uc.Tag = this.ucConsole1;
                    if (uc is SVMUserControl)
                        ((SVMUserControl)uc).TaskListForm = taskListForm;
                }
                menus[i].Click += new EventHandler(btnMenuClick);
            }
            var ISVMHosts = ucControls.Where(o => o is ISVMTaskHost).Select(o => o as ISVMTaskHost).ToList();
            ISVMHosts.ForEach(o => o.TaskAdded += ((os, oe) => taskListForm.UCTaskView.Add(oe.Task)));

            taskListForm.UCTaskView.Tag = this.ucConsole1;

            btnTrainPredict.PerformClick();
        }

        void btnMenuClick(object sender, EventArgs e) {
            KryptonCheckButton menu = sender as KryptonCheckButton;
            UserControl panel = menu.Tag as UserControl;
            if (panel == null) {
                menu.Checked = false;
                return;
            }
            foreach (var item in menus) {
                item.Checked = false;
            }
            menu.Checked = true;
            spliterRightPanel.Panel1.Controls.Clear();
            spliterRightPanel.Panel1.Controls.Add(panel);
            if (menu.Text.Contains("SVM"))
                panel.Controls.Add(UCSVMSetting.SingleInstance);
        }

        private void rightTopToolStripMenuItem_Click(object sender, EventArgs e) {
            spliterRightPanel.Panel1Collapsed = !spliterRightPanel.Panel1Collapsed;
        }

        private void toolStripMenuItemLeftMenuGroup_Click(object sender, EventArgs e) {
            spliterMain.Panel1Collapsed = !spliterMain.Panel1Collapsed;
        }

        private void taskListTToolStripMenuItem_Click(object sender, EventArgs e) {
            if (taskListForm.Visible)
                taskListForm.Hide();
            else
                taskListForm.Show();
        }
    }
}